#include "gamepushbutton.h"
#include <QPropertyAnimation>
GamePushButton::GamePushButton(QWidget *parent) : QPushButton(parent)
{

}

GamePushButton::GamePushButton(QString m_imgNormal, QString m_imgPressed)
{
    this->m_imgNormal = m_imgNormal;
    this->m_imgPressed = m_imgPressed;
    QPixmap buttonpix;
    bool result = buttonpix.load(m_imgNormal);
    if(result){
         QSize qSize(buttonpix.width(),buttonpix.height());
        this->setFixedSize(qSize);
        this->setStyleSheet("QPushButton{border:0px}");
        this->setIcon(buttonpix);
        this->setIconSize(qSize);
    }
}

//向上跳
void GamePushButton::zoomUp(){
    //创建动态对象
    QPropertyAnimation* animation = new QPropertyAnimation(this,"geometry");
    //设置持续时间
    animation->setDuration(500);
    //设置开始位置
    animation->setStartValue(QRect(this->x(),this->y(),this->width(),this->height()));
    //设置结束位置
    animation->setEndValue(QRect(this->x(),this->y()+10,this->width(),this->height()));
    //设置曲线动画
    animation->setEasingCurve(QEasingCurve::OutBounce);
    animation->start();
}

//向下跳
void GamePushButton::zoomDown(){
    //创建动态对象
    QPropertyAnimation* animation = new QPropertyAnimation(this,"geometry");
    //设置持续时间
    animation->setDuration(500);
    //设置开始位置
    animation->setStartValue(QRect(this->x(),this->y()+10,this->width(),this->height()));
    //设置结束位置
    animation->setEndValue(QRect(this->x(),this->y(),this->width(),this->height()));
    //设置曲线动画
    animation->setEasingCurve(QEasingCurve::OutBounce);
    animation->start();

}

void GamePushButton::mousePressEvent(QMouseEvent *e){
    if(m_imgPressed != ""){
        QPixmap pix;
        bool ret = pix.load(this->m_imgPressed);
        if(ret){
            this->setFixedSize(pix.width(),pix.height());

            this->setStyleSheet("QPushButton{border:0px}");
            this->setIcon(pix);
            this->setIconSize(QSize(pix.width(),pix.height()));
        }
    }
    return QPushButton::mousePressEvent(e);
}
void GamePushButton::mouseReleaseEvent(QMouseEvent *e){
    if(m_imgPressed != ""){
        QPixmap pix;
        bool ret = pix.load(this->m_imgNormal);
        if(ret){
            this->setFixedSize(pix.width(),pix.height());

            this->setStyleSheet("QPushButton{border:0px}");
            this->setIcon(pix);
            this->setIconSize(QSize(pix.width(),pix.height()));
        }
    }
    return QPushButton::mouseReleaseEvent(e);
}
